package com.gskj.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gskj.commonutils.ResultJson;
import com.gskj.eduservice.entity.EduTeacher;
import com.gskj.eduservice.entity.vo.TeacherQuery;
import com.gskj.eduservice.service.IEduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-13
 */
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin// 解决跨域问题
public class EduTeacherController {

    // myBatisPlus 也对 service进行了封装。效果与mapper一样
    @Autowired
    private IEduTeacherService iEduTeacherService;

    /***
     *  查询 讲师集合
     * @return
     */
    @GetMapping("/findAll")
    public ResultJson getTeacherList() {
        List<EduTeacher> list = iEduTeacherService.list(null);

        ResultJson teacherList = ResultJson.success().data("teacherList", list);
        return teacherList;
    }

    /***
     *   根据 id 删除 id 讲师
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResultJson removeTeacherById(
            @PathVariable("id") String id) {
        boolean b = iEduTeacherService.removeById(id);

        if (b) {
            return ResultJson.success();
        }
        return ResultJson.error();
    }


    /***
     *  分页查询
     * @param page  当前页码
     * @param size 每页显示条数
     * @return
     */
    @GetMapping("/page/teacher/{page}/{size}")
    public ResultJson pageTeacher(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size) {

        //创建 page对象
        Page<EduTeacher> teacherPage = new Page<>(page, size);

        // 调用service
        // 此方法调用后。底层会将分页的所有数据 封装到 teacherPage对象中
        iEduTeacherService.page(teacherPage, null);

        long total = teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();

        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", records);
        // 返回
        return ResultJson.success().data(map);
    }

    /***
     *  分页+条件查询
     * @param page  页码
     * @param size  条数
     * @param teacherQuery  查询条件
     * @return
     */
    @PostMapping("/page/teacher/condition/{page}/{size}")
    public ResultJson pageTeacherCondition(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size,
            //查询条件
            @RequestBody(required = false) TeacherQuery teacherQuery) {

        // 创建page对象
        Page<EduTeacher> teacherPage = new Page<>(page, size);

        //构造 查询条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //判断条件值是否为空
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        Integer level = teacherQuery.getLevel();
        String name = teacherQuery.getName();

        // 当查询条件不为空， 拼接查询条件
        if (!StringUtils.isEmpty(name)) {
            // 模糊条件 构建
            wrapper.like("name", name);
        }

        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }

        if (!StringUtils.isEmpty(begin)) {
            // 大于等于 ...
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            // 小于等于 ...
            wrapper.le("gmt_modified", end);
        }

        wrapper.orderByDesc("gmt_create");
        // 调用service
        iEduTeacherService.page(teacherPage, wrapper);

        long total = teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();

        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", records);
        // 返回
        return ResultJson.success().data(map);
    }

    /***
     *  添加讲师
     * @param eduTeacher
     * @return
     */
    @PostMapping("/add/teacher")
    public ResultJson addTeacher(
            @RequestBody EduTeacher eduTeacher) {

        boolean save = iEduTeacherService.save(eduTeacher);
        if (save)
            return ResultJson.success();

        return ResultJson.error();
    }

    /***
     *  根据id  查询讲师
     * @param id
     * @return
     */
    @GetMapping("/get/teacher/{id}")
    public ResultJson getTeacherById(
            @PathVariable("id") String id) {
//
//        try {
//            int i = 1 / 0;
//        } catch (Exception e) {
//            throw new GuLiException(20001, "自定义异常处理数字异常");
//        }

        EduTeacher byId = iEduTeacherService.getById(id);
        if (byId != null)
            return ResultJson.success().data("teacher", byId);
        return ResultJson.error().message("没有查询到该讲师的信息");
    }

    /***
     *  根据 id修改 讲师 信息
     * @param eduTeacher
     * @return
     */
    @PostMapping("/update/teacher")
    public ResultJson updateTeacher(
            @RequestBody EduTeacher eduTeacher) {
        boolean b = iEduTeacherService.updateById(eduTeacher);
        if (b)
            return ResultJson.success();
        return ResultJson.error().message("修改失败！");
    }
}

